package project.ia04.gui.components;

import jade.gui.GuiEvent;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;

import project.ia04.agents.gui.XAgent;
import project.ia04.gui.frames.MainFrame;
import project.ia04.gui.frames.ScreenSettings;

public class HistoricalDialog extends JDialog {

	private static HistoricalDialog singleton;

	private JScrollPane scrollPane;
	
	private GridBagLayout gameLayout = new GridBagLayout();
	private JPanel gamePanel = new JPanel(gameLayout);
	
	private ArrayList<VideoGamePanel> gamePanelList = new ArrayList<VideoGamePanel>();
	private int gridCurrY = 0;
	
	private JFrame frame;
	
	private HistoricalDialog(JFrame frame){
		setSize(500,300);
		setLocation(
				(ScreenSettings.getInstance().getCenterPosition().x - 250),
				(ScreenSettings.getInstance().getCenterPosition().y- 150));
		setResizable(false);
		this.frame = frame;
		scrollPane = new JScrollPane(gamePanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setColumnHeaderView(new JLabel("Jeux",JLabel.CENTER));
		
		
		add(scrollPane);
		
		buildTheme();
	}
	
	public static HistoricalDialog getInstance(JFrame frame){
		if(singleton == null)
			singleton = new HistoricalDialog(frame);
		return singleton;
	}
	
	public void reinit(){
		for(int i=0;i<gamePanelList.size();i++){
			this.gamePanel.remove(gamePanelList.get(i));
		}
	}
	
	public void addGame(String title,String genre,String type,float cost,int unitSell,float profits){
		VideoGamePanel videoGamePanel = new VideoGamePanel(title, genre, type, cost, unitSell, profits);
		
		GridBagConstraints c; 
		if(gamePanelList.size() > 0){
			c = gameLayout.getConstraints(gamePanelList.get(gamePanelList.size()-1));
			c.weighty = 0.0;
			gameLayout.setConstraints(gamePanelList.get(gamePanelList.size()-1),c);
		}
		
		c = new GridBagConstraints();
		c.gridy = gridCurrY++;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		gamePanel.add(videoGamePanel,c);
		gamePanelList.add(videoGamePanel);
		scrollPane.validate();
	}
	
	public void buildTheme(){
		gamePanel.setBackground(Color.DARK_GRAY);
		gamePanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,new Color(102,102,102),Color.WHITE));
	}
	
	public void showIt(){
		GuiEvent e = new GuiEvent(this,XAgent.GETHISTORICAL);
		((MainFrame)frame).getXAgent().postGuiEvent(e);
		//setVisible(true);
	}
}
